<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */

// DON't USE THIS ANYMORE!!!

class Discovery_WithJID {
    
    private $uri;
    private $domain;
    
    private $hub;
    private $salmon;
    private $atom;
    private $magicPublicKey;
    
    private $webfingerProfilePage;
    
    private $feed;
    
    public $debug = false;
    
    public function __construct($uri) {
        
        $uri = urldecode($uri);
        if(substr($uri, 0, 4) == 'http') {
            $domain = parse_url($uri, PHP_URL_HOST);
            $this->setDomain($domain);
        } else {
            list($node, $domain) = explode('@', $uri);
            $this->setDomain($domain);
        }
        $this->setUri($uri);
    }
    
    private function setUri($uri) {
        if(empty($uri)) {
            throw new Exception("URI cannot be set empty!");
        }
        $this->uri = $uri;
    }
    
    private function setDomain($domain) {
        if(empty($domain)) {
            throw new Exception("Domain cannot be set empty!");
        }
        $this->domain = $domain;
    }
    
    private function getDomain($domain) {
        return $this->domain = $domain;
    }
    
    private function setFeed(Atom_Feed $feed) {
        if(empty($feed)) {
            throw new Exception("Feed cannot be set empty!");
        }
        $this->feed = $feed;
    }
    
    /**
     * @return Atom_Feed
     */
    public function getFeed() {
        return $this->feed;
    }
    
    public function discover() {
        
        $rawHostMetaDataXML = $this->fetchURL($this->generateWellKnowHostMetaURL());
        
        if($this->debug) {
            echo "\nHost-Meta Data: " .$rawHostMetaDataXML;
        }
        
        $hostMetaData = Xrd_HostMeta::buildFromRawXML($rawHostMetaDataXML);
        
        if($hostMetaData->getHostValue() != $this->domain) {
            throw new Exception("Host value(" . $hostMetaData->getHostValue() . ") of the HostMetaData does not match the users (" . $this->domain . ").");
        }
        
        $rawLrddXML = $this->fetchURL($hostMetaData->generateUserXrdURL($this->uri)); 
        
        if($this->debug) {
            echo "\nLRDD : " .$rawLrddXML;
        }
        
        $lrdd = Xrd_Lrdd::buildFromRawXML($rawLrddXML);
        //echo $lrdd->asXML();
        // Why o why there is a if ... if there is no atom, subscription is useless.
        if($lrdd->getLinkAtom()->getHref()) {
            $rawAtomFeed = $this->fetchURL($lrdd->getLinkAtom()->getHref());
            
            if($this->debug) {
                echo "\nFeed : " .$rawAtomFeed;
            }
            
            $feed = Atom_Feed::buildFromRawXML($rawAtomFeed);
            $this->setHub($feed->getHub());
            $this->setFeed($feed);
        }
            
        $this->setAtom($lrdd->getLinkAtom());
        $this->setSalmon($lrdd->getLinkSalmon());
        $this->setMagicPublicKey($lrdd->getLinkMagicPublicKey());
        $this->setWebfingerProfilePage($lrdd->getLinkWebfingerProfilePage());
    }
    
    private function setHub($hub) {
        if(empty($hub)) {
            throw new Exception("Hub cannot be set empty!");
        }
        $this->hub = $hub;
    }
    
    private function setAtom($atom) {
        if(empty($atom)) {
            throw new Exception("Atom cannot be set empty!");
        }
        $this->atom = $atom;
    }
    
    private function setSalmon($salmon) {
        if(empty($salmon)) {
            throw new Exception("Salmon cannot be set empty!");
        }
        $this->salmon = $salmon;
    }
    
    private function setMagicPublicKey($mpk) {
        if(empty($mpk)) {
            throw new Exception("MagicPublicKey cannot be set empty!");
        }
        $this->magicPublicKey = $mpk;
    }
    
    private function setWebfingerProfilePage($wpp) {
        if(empty($wpp)) {
            throw new Exception("WebfingerProfilePage cannot be set empty!");
        }
        $this->webfingerProfilePage = $wpp;
    }
    
    /**
     * @return Xrd_Lrdd_Link
     */
    public function getWebfingerProfilePage() {
        return $this->webfingerProfilePage;
    }
    
    
    public function getHub() {
        return $this->hub; 
    }
    
    public function getAtom() {
        return $this->atom; 
    }
    
    public function getSalmon() {
        return $this->salmon; 
    }
    
    public function getMagicPublicKey() {
        return $this->magicPublicKey; 
    }
    
    //TODO add httpFetch handler so that the 'engine' to do the fetch
    // can be overriden.
    protected function fetchURL($url) {
        return file_get_contents($url);
    }
    
    protected function generateWellKnowHostMetaURL() {
        return 'http://' . $this->domain . '/.well-known/host-meta';
    }
}